/*
 * Background Flickr: Copyright (C) 2009  Craig McNicholas
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.ddw.app.bflickr.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.ddw.app.bflickr.logging.Log;
import com.ddw.app.bflickr.logging.LogFactory;

/**
 * The SQL database adapter to handle creating/opening/closing a
 * database.
 * @author craig
 * 
 */
public class DatabaseAdapter {
	
	private Log log = LogFactory.createInstance(DatabaseHelper.class);
	private Context context;
	private DatabaseHelper helper;
	private SQLiteDatabase database = null;

	/**
	 * Constructs a new database adapter for connecting and running SQL 
	 * commands against a database
	 * @param context the context of the application
	 * @param databaseName the databases name
	 * @param databaseVersion the databases version
	 * @param createStatement the SQL statement to create the database tables
	 * @param destroyStatement the SQL statement to destroy the database tables
	 */
	public DatabaseAdapter(Context context, String databaseName, int databaseVersion, String createStatement, String destroyStatement) {
		this.context = context;
		this.helper = new DatabaseHelper(this.context, databaseName, databaseVersion, createStatement, destroyStatement);
	}
	
	/**
	 * Opens the SQL database connection
	 */
	public SQLiteDatabase open() {
		if(this.database == null) {
			log.debug("Database is null, opening connection to " + this.helper.getDatabaseName());
			this.database = this.helper.getWritableDatabase();
			return this.database;
		} else if(!this.database.isOpen()) {
			log.debug("Opening connection to " + this.helper.getDatabaseName());
			this.database = this.helper.getWritableDatabase();
			return this.database;
		} else {
			log.warn("Connection is already open");
			return this.database;
		}
	}
	
	/**
	 * Get the active database object or null if the database
	 * has not been instantiated
	 * @return the database object or null
	 */
	public SQLiteDatabase getDatabase() {
		return this.database;
	}
	
	/**
	 * Closes the SQL database connection
	 */
	public void close() {
		if(this.database == null) {
			log.warn("Connection is already closed as the database is null");
		} else if(this.database.isOpen()) {
			log.debug("Closing connection to " + this.helper.getDatabaseName());
			this.database.close();
		} else {
			log.warn("Connection is already closed");
		}
	}

}
